﻿// Copyright (c) egmkang wang. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace XServer.Message
{
    using System;
    using System.IO;

    public interface IMessage
    {
        object Deserialize(byte[] bytes);
        object Deserialize(Stream stream);

        void Serialize(Stream stream);
        byte[] Serialize();
    }

    public class MessageBase<T> : IMessage
        where T : class, new()
    {
        public object Deserialize(byte[] bytes)
        {
            return MessagePack.LZ4MessagePackSerializer.Deserialize<T>(bytes);
        }

        public object Deserialize(Stream stream)
        {
            return MessagePack.LZ4MessagePackSerializer.Deserialize<T>(stream);
        }

        public void Serialize(Stream stream)
        {
            MessagePack.LZ4MessagePackSerializer.Serialize<T>(stream, this as T);
        }
        public byte[] Serialize()
        {
            return MessagePack.LZ4MessagePackSerializer.Serialize<T>(this as T);
        }
    }
}
